package com.ashion.findfuny;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ashion.findfuny.AsyncImageLoader.ImageCallback;

public class MainListAdapter extends BaseAdapter{

	private static final String TAG = MainListAdapter.class.getSimpleName();
	private Context context;
	private LayoutInflater mInflater;
	private List<Map<String, Object>> mListData;
	private AsyncImageLoader asyncImageLoader;
	private ListView listView;
	private boolean isShowProgress;
	private ProgressBar progress;
	
    public MainListAdapter(Context context,ListView listView, List<Map<String, Object>> mListData){
        this.mInflater = LayoutInflater.from(context);
        this.mListData = mListData;
        this.context = context;
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mListData.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setProgressVisiable(boolean flag){
    	progress.setVisibility(flag?View.VISIBLE:View.GONE);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        ViewHolder holder = null;
        if (convertView == null) {
             
            holder=new ViewHolder();  
             
            convertView = mInflater.inflate(R.layout.main_list_item, null);
            holder.image = (ImageView)convertView.findViewById(R.id.item_image);
            holder.title = (TextView)convertView.findViewById(R.id.item_title);
            holder.user = (TextView)convertView.findViewById(R.id.item_user);
            holder.count = (TextView)convertView.findViewById(R.id.item_count);
            holder.progress = (ProgressBar)convertView.findViewById(android.R.id.progress);
            
            convertView.setTag(holder);
            convertView.setClickable(true);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        
        if(position == getCount()-1){
        	progress = holder.progress;
        	holder.progress.setVisibility(View.VISIBLE);
        }else{
        	holder.progress.setVisibility(View.GONE);
        }
        
        Map<String, Object> map = mListData.get(position); 
        
        String imageUrl = (String)map.get(CommonUtil.IMAGE);
		//holder.image.setBackgroundResource((Integer)map.get(CommonUtil.IMAGE));
        holder.image.setTag(position);

        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl, int position) {
                ImageView imageViewByTag = (ImageView)listView.findViewWithTag(position);
                if (imageViewByTag != null && imageDrawable!=null) {
                	//Log.v(TAG, "--------------setbackground null");
                	imageViewByTag.setBackgroundResource(0);
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        }, position);
        
		if (cachedImage != null) {
			holder.image.setImageDrawable(cachedImage);
		}else{
			holder.image.setBackgroundResource(R.drawable.list_item_image_default);
		}
        
        holder.title.setText((String)map.get(CommonUtil.TITLE));
        holder.user.setText((String)map.get(CommonUtil.USER));
        holder.count.setText(position+"");
        
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	Log.v(TAG, "ashion log------------>item onclick");    
            	Intent intent = new Intent(context, DetailActivity.class);
            	context.startActivity(intent);
            }
        });
        
        if(map.get(CommonUtil.HAS_VISIABLE) == null){
        	map.put(CommonUtil.HAS_VISIABLE, "true");
        	TranslateAnimation anim = new TranslateAnimation(0, 0, 200, 0);
        	anim.setDuration(500);
        	/*anim.setInterpolator(AnimationUtils.loadInterpolator(context,
                    android.R.anim.accelerate_decelerate_interpolator));*/
        	convertView.startAnimation(anim);
        }
         
        return convertView;
    }
     
    
    class ViewHolder{
    	private ImageView image;
    	private TextView title;
    	private TextView user;
    	private TextView count;
    	private ProgressBar progress;
    }
}
